package com.lm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class LMControlPanel implements ActionListener, AdjustmentListener, MouseListener {

    private Main comp;
    private TableModel tableModel;
    private JTable table;
    private LayerContainer layerContainer;
    private JPopupMenu LMPopupMenu;
    private JMenuItem MoveUp;
    private JMenuItem MoveDown;
    private JMenuItem Remove;
    private JMenuItem AddNew;
    private int horX;
    private int verY;
    private int width;
    private int height;

    public LMControlPanel(Main comp) {
        this.comp = comp;
        LMPopupMenu = new JPopupMenu();
        AddNew = new JMenuItem("New Layer");
        LMPopupMenu.add(AddNew);
        AddNew.addActionListener(this);
        Remove = new JMenuItem("Remove Layer");
        LMPopupMenu.add(Remove);
        Remove.addActionListener(this);
        MoveUp = new JMenuItem("Move Up");
        LMPopupMenu.add(MoveUp);
        MoveUp.addActionListener(this);
        MoveDown = new JMenuItem("Move Down");
        LMPopupMenu.add(MoveDown);
        MoveDown.addActionListener(this);
        layerContainer = new LayerContainer();

        tableModel = new AbstractTableModel() {

            String col[] = {"Visible", "Layer Name"};

            public int getColumnCount() {
                return col.length;
            }

            public int getRowCount() {
                return layerContainer.getLayers().size();
            }

            public Object getValueAt(int row, int col) {
                LayerManager lc = (LayerManager) layerContainer.getLayers().get(row);
                return (col == 0) ? lc.isShow() : lc.getName();
            }

            public boolean isCellEditable(int row, int col) {
                return true;
            }

            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                LayerManager lc = (LayerManager) layerContainer.getLayers().get(rowIndex);
                if (columnIndex == 0) {
                    lc.setShow(lc.isShow() ? false : true);
                } else if (!layerContainer.getLayersMapping().contains(aValue.toString().trim())) {
                    lc.setName(aValue.toString().trim());
                }
            }

            public Class getColumnClass(int columnIndex) {
                return (columnIndex == 0) ? Boolean.class : String.class;
            }

            public String getColumnName(int column) {
                return col[column];
            }
        };
        table = new JTable(tableModel);
        table.setComponentPopupMenu(LMPopupMenu);
        table.getColumnModel().getColumn(0).setMaxWidth(50);

        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        comp.ScrollPanePanelLayerManager.setViewportView(getTable());
        comp.ScrollPanePanelLayerManager.setComponentPopupMenu(LMPopupMenu);

        table.addMouseListener(this);

        comp.ScrollPaneWorkspaceRender.getHorizontalScrollBar().addAdjustmentListener(this);
        comp.ScrollPaneWorkspaceRender.getVerticalScrollBar().addAdjustmentListener(this);
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa");
    }

    public JTable getTable() {
        return table;
    }

    public void actionPerformed(ActionEvent e) {
        if (!comp.project.isCreated()) {
            // return;
        }
        Object ob = e.getSource();
        int indexRow = table.getSelectedRow();
        if (ob == AddNew) {
            int i = 1;
            while (true) {
                String layerName = "Layer " + i;
                if (!layerContainer.getLayersMapping().contains(layerName)) {
                    layerContainer.addLayer(layerName, true);
                    System.out.println(layerName + " = " + layerContainer.getLayersMapping().contains(layerName));
                    break;
                }
                i++;
            }
        } else if (ob == Remove) {
            try {
                layerContainer.getLayers().remove(indexRow);
                layerContainer.getLayersMapping().remove(indexRow);
            } catch (Exception err) {
            }
        } else if (ob == MoveUp) {
            if (indexRow != -1 && indexRow != 0) {
                Object tmp = layerContainer.getLayers().get(indexRow);
                layerContainer.getLayers().set(indexRow, layerContainer.getLayers().get(indexRow - 1));
                layerContainer.getLayers().set(indexRow - 1, tmp);
                table.setColumnSelectionInterval(0, 1);
                table.setRowSelectionInterval(indexRow - 1, indexRow - 1);
            }
        } else if (ob == MoveDown) {
            if (indexRow != -1 && indexRow != layerContainer.getLayers().size() - 1) {
                Object tmp = layerContainer.getLayers().get(indexRow);
                layerContainer.getLayers().set(indexRow, layerContainer.getLayers().get(indexRow + 1));
                layerContainer.getLayers().set(indexRow + 1, tmp);
                table.setColumnSelectionInterval(0, 1);
                table.setRowSelectionInterval(indexRow + 1, indexRow + 1);
            }
        }
        table.repaint();
        table.revalidate();


    }

    public void adjustmentValueChanged(AdjustmentEvent e) {

        horX = comp.ScrollPaneWorkspaceRender.getHorizontalScrollBar().getValue();
        verY = comp.ScrollPaneWorkspaceRender.getVerticalScrollBar().getValue();
        width = horX + comp.ScrollPaneWorkspaceRender.getVisibleRect().width;
        height = verY + comp.ScrollPaneWorkspaceRender.getVisibleRect().height;
        drawLayer();

        System.out.println("Layer================================");
        System.out.println("horX = " + horX + " verY = " + verY);
        System.out.println("Visible w = " + comp.ScrollPaneWorkspaceRender.getVisibleRect().width + " Vis h = " + comp.ScrollPaneWorkspaceRender.getVisibleRect().height);
        System.out.println("View W = " + width + " View H = " + height);


    }

    public void mouseClicked(MouseEvent e) {
        int indexRow = table.getSelectedRow();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void drawLayer() {
        for (int i = 0; i < layerContainer.getLayers().size(); i++) {
            LayerManager lm = (LayerManager) layerContainer.getLayers().get(i);
            if (lm.isShow()) {
                drawLayerManager(lm);
            }
        }
    }

    public void drawLayerManager(LayerManager lm) {
    }
}
